package com.jjvu.news.controller;

import com.jjvu.news.dao.Info;
import com.jjvu.news.dao.MaxMenu;
import com.jjvu.news.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private InfoService indexService;
    @RequestMapping("/toIndex")
    public String toIndex(Model model){
        List<Info> info1List=indexService.findInfoListById(1);
        model.addAttribute("info1List",info1List);
        List<Info> info2List=indexService.findInfoListById(2);
        model.addAttribute("info2List",info2List);
        findIdex(model);
        return "index";
    }

    //列表title信息对应的shownews
    @RequestMapping("/showNews")
    public String shownes(Integer info_id, Model model){
        findIdex(model);
        Info info=indexService.findInfoById(info_id);
        model.addAttribute("info",info);
        return "shownews";
    }
    @RequestMapping("/allNews")
    public String allnews(String title,Integer maxid,Integer minid,Model model){
        findIdex(model);
        try {
            title=new String(title.getBytes("iso-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        model.addAttribute("title",title);
        List<Info> allNews= indexService.findAllNews(maxid,minid);
        model.addAttribute("allNews",allNews);
        return "allnews";
    }

    private void findIdex(Model model) {
        //列表title信息
        List<Info> info3List=indexService.findInfoListById(3);
        model.addAttribute("info3List",info3List);
        List<Info> info4List=indexService.findInfoListById(4);
        model.addAttribute("info4List",info4List);
        //导航栏
        List<MaxMenu> menuList=indexService.findMaxMenuList();
        model.addAttribute("menuList",menuList);
    }
}
